/**
 * Created by Chris on 14-2-15.
 */
define(['text!login/service.html', 'login/data/homelist', 'login/swipe'],
    function(viewTemplate, HomeList) {
        return Piece.View.extend({
            id: 'login_logined',
            events: {
                "click #instructions": "instructions",
                "click #standards": "standards",
                "click #tips": "tips",
                "click #user": "user",
            },
            render: function() {
                $(this.el).html(viewTemplate);

                Piece.View.prototype.render.call(this);
                return this;
            },
            onShow: function() {

                this.loadModuleList(HomeList);

            },

            isOver: 0,

            loadModuleList: function(data) {
                var that = this;

                var me = this;
                if (that.isOver === 0) {

                    that.isOver = that.isOver + 1;
                    var i = 0;
                    $("#swipe").html("");
                    $("#position").html("");
                    console.log('AAAAAA------------------ZHE');

                    data = data.modules;
                    //$.parseJSON(data);

                    var valueArray = new Array;
                    var keyArray = new Array;
                    var s = 3;

                    _.each(data, function(value, key) {

                        // console.info("key===="+value.category);
                        if (value.category === "首页") {

                            keyArray[0] = value.category;
                            valueArray[0] = value;
                        } else if (value.category === "次页") {

                            keyArray[1] = value.category;
                            valueArray[1] = value;
                        } else if (value.category === "基础包") {
                            keyArray[2] = value.category;
                            valueArray[2] = value;
                        } else {

                            keyArray[s] = value.category;
                            valueArray[s] = value;
                            s++;
                        }
                        // console.log(s);

                    });
                    $("#position").html("");


                    console.info(valueArray);
                    _.each(valueArray, function(value) {

                        console.info(value);
                        var len = value.length;
                        var j = 0;

                        //排列

                        value = _.sortBy(value, function(v) {
                            console.info(v.sortingWeight);
                            return v.sortingWeight;
                        });

                        _.each((value), function(value, key) {
                            console.info(value);
                            if (j % 12 === 0) {
                                i++;
                                $("#swipe").append("<div class='page_div'><ul id='myul" + i + "' clsss='scrollContent'></ul></div>");


                                $("#position").append("<li></li>");

                            }

                            console.log("aaaa --" + value.sortingWeight + "");

                            $("#myul" + i).append(
                                _.template($("#module_div_ul").html(), {
                                    'icon': value.icon,
                                    'name': value.name,
                                    'identifier': value.identifier,
                                    'moduleType': value.moduleType,
                                    'msgCount': 0
                                }));

                            j++;

                        });

                    });



                    if (isShowBullet) {
                        $("#position").children("li:nth-child(1)").addClass("on");
                    }

                    console.log("i=" + i);

                    that.isOver = that.isOver - 1;



                }

            },



        }); //view define

    });